﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using HA.ModFlow.Domain.DomainLayer;

using HA.Data;

namespace HA.ModFlow.Controls.Windows
{
    /// <summary>
    /// winGridConfig.xaml 的交互逻辑
    /// </summary>
    public partial class winGridConfig : Window
    {
        //public winGridConfig(GridData data)
        //{
        //    InitializeComponent();

            

        //    _gridDef = new GridDefinition(data);

        //    this.DataContext = _gridDef;




        //}


        public winGridConfig(GridConfig config)
        {

            InitializeComponent();



            _gridDef = new GridDefinition(config);

            this.DataContext = _gridDef;



        }

        
        GridData _gridData;

        public GridData GridData
        {
            get { return _gridData; }
           
        }

        GridDefinition _gridDef;


     
     



        private void bntOk_Click(object sender, RoutedEventArgs e)
        {
            if (this.CheckData() == true)
            {
                this.DialogResult = true;
                this._gridData = this._gridDef.GetNewGridData();

            }
            else
            {
                this.DialogResult = false ;
            
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (this._gridDef.Config.IsRefinedPointMode)
            {
                this.txtXGridCount.IsEnabled = false;
                this.txtBiassX.IsEnabled = false;
                this.chkIsUseBLX.IsEnabled = false;
                this.txtBaseCellSizeX.IsEnabled = false;
                this.txtLimitCellSizeX.IsEnabled = false;
                this.txtYGridCount.IsEnabled = false;
                this.txtBiassY.IsEnabled = false;
                this.chkIsUseBLY.IsEnabled = false;
                this.txtBaseCellSizeY.IsEnabled = false;
                this.txtLimitCellSizeY.IsEnabled = false;
            }
        }

        /// <summary>
        /// 进行必要的数据有效性的检验
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            if (this._gridDef.Config.IsRefinedPointMode == false)
            {
                if (this._gridDef.Config.LengthX <= 0 || this._gridDef.Config.LengthY <= 0 || this._gridDef.Config.LengthZ <= 0)
                {
                    MessageBox.Show("您设置的网格长度可能有错误，请检查！将取消本次操作！");
                    return false;

                }
                if (this._gridDef.Config.IsUseBLX == false && this._gridDef.Config.CellX <= 0)
                {
                    MessageBox.Show("您设置的网格X方向单元格个数可能有错误，请检查！将取消本次操作！");
                    return false;
                }
                if (this._gridDef.Config.IsUseBLY == false && this._gridDef.Config.CellY <= 0)
                {
                    MessageBox.Show("您设置的网格Y方向单元格个数可能有错误，请检查！将取消本次操作！");
                    return false;
                }
                if (this._gridDef.Config.IsUseBLZ == false && this._gridDef.Config.CellZ <= 0)
                {
                    MessageBox.Show("您设置的网格Z方向单元格个数可能有错误，请检查！将取消本次操作！");
                    return false;
                }
                if (this._gridDef.Config.IsUseBLX == true)
                {
                    if (this._gridDef.Config.BaseCellSizeX <= 0 || this._gridDef.Config.LimitCellSizeX <= 0)
                    {
                        MessageBox.Show("您设置的网格X方向单元格长度可能有错误，请检查！将取消本次操作！");
                        return false;
                    }
                    if (this._gridDef.Config.BaseCellSizeX > this._gridDef.Config.LimitCellSizeX)
                    {
                        MessageBox.Show("您设置的网格X方向单元格最大长度小于基础长度，请检查！将取消本次操作！");
                        return false;
                    }
                }
                if (this._gridDef.Config.IsUseBLY == true)
                {
                    if (this._gridDef.Config.BaseCellSizeY <= 0 || this._gridDef.Config.LimitCellSizeY <= 0)
                    {
                        MessageBox.Show("您设置的网格Y方向单元格长度可能有错误，请检查！将取消本次操作！");
                        return false;
                    }
                    if (this._gridDef.Config.BaseCellSizeY > this._gridDef.Config.LimitCellSizeY)
                    {
                        MessageBox.Show("您设置的网格Y方向单元格最大长度小于基础长度，请检查！将取消本次操作！");
                        return false;
                    }
                }
                if (this._gridDef.Config.IsUseBLZ == true)
                {
                    if (this._gridDef.Config.BaseCellSizeZ <= 0 || this._gridDef.Config.LimitCellSizeZ <= 0)
                    {
                         MessageBox.Show("您设置的网格Z方向单元格长度可能有错误，请检查！将取消本次操作！");
                        return false;
                    }
                    if (this._gridDef.Config.BaseCellSizeZ > this._gridDef.Config.LimitCellSizeZ)
                    {
                        MessageBox.Show("您设置的网格Z方向单元格最大长度小于基础长度，请检查！将取消本次操作！");
                        return false;
                    }
                }

            }
            else
            {
                if (this._gridDef.Config.LengthX <= 0 || this._gridDef.Config.LengthY <= 0 || this._gridDef.Config.LengthZ <= 0)
                {
                    MessageBox.Show("您设置的网格长度可能有错误，请检查！将取消本次操作！");
                    return false;
                }
                if (this._gridDef.Config.IsUseBLZ == false && this._gridDef.Config.CellZ <= 0)
                {
                    MessageBox.Show("您设置的网格Z方向单元格个数可能有错误，请检查！将取消本次操作！");
                    return false;
                }
                if (this._gridDef.Config.IsUseBLZ == true)
                {
                    if (this._gridDef.Config.BaseCellSizeZ <= 0 || this._gridDef.Config.LimitCellSizeZ <= 0)
                    {
                        MessageBox.Show("您设置的网格Z方向单元格长度可能有错误，请检查！将取消本次操作！");
                        return false;
                    }
                    if (this._gridDef.Config.BaseCellSizeZ > this._gridDef.Config.LimitCellSizeZ)
                    {
                        MessageBox.Show("您设置的网格Z方向单元格最大长度小于基础长度，请检查！将取消本次操作！");
                        return false;
                    }
                }

            }

            return true;

        }


    }
}
